package Componentes.RendersValidators;

import Configuraciones.Configuraciones;
import Modelos.TblClientes;
import Modelos.TblPerfiles;
import java.awt.Component;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;


public class ButtonRenderClientes  extends JLabel implements TableCellRenderer, RenderValidators{

    private boolean isBordered = true;
   

    public ButtonRenderClientes() {
        setOpaque(true);
    }
    
    
    

    public ButtonRenderClientes(boolean isBordered) {
        this();
        this.isBordered = isBordered;
        
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus, int row, int column) {
        //perfil
        String val=(String)table.getModel().getValueAt(row, 1);
        if(!Rfc(val))
                return new JButton(Configuraciones.ICONO_WARNING);
        
          //Nombre 
        val=(String)table.getModel().getValueAt(row, 2);
        if(!Nombre(val))
            return new JButton(Configuraciones.ICONO_WARNING);
        
        //Descripción
        val=(String)table.getModel().getValueAt(row, 3);
        if(!Apellido(val))
            return new JButton(Configuraciones.ICONO_WARNING);

        //Estatus
        boolean estatus = (Boolean)table.getModel().getValueAt(row, 5);
        if(estatus)
            return new JButton(Configuraciones.ICONO_REG_OK);
        else
            return new JButton(Configuraciones.ICONO_INFO);

    }


    public boolean isIsBordered() {
        return isBordered;
    }


    public void setIsBordered(boolean isBordered) {
        this.isBordered = isBordered;
    }

  

    @Override
    public boolean isSave(List ls) {
        boolean isOk=true;
        for (Object object : ls) {
            TblClientes cliente=(TblClientes) object;
            
            isOk=isOk && Rfc(cliente.getRfc());
            isOk=isOk && Nombre(cliente.getNombre());
            isOk=isOk && Apellido(cliente.getApellidop());
            if(!isOk) return false;
        }
        return true;
    }
    

    public boolean Rfc(String val){
        boolean isValid=true;
        isValid= val.trim().length()==13 || val.trim().length()==12;
        if(isValid){
            Pattern p= Pattern.compile("[a-zA-Z_0-9]*");
            Matcher m= p.matcher(val);
            isValid=m.matches();
        }else isValid=false;
    return isValid;
    }
    
    
    public boolean Nombre(String val){
        boolean isValid=true;
        if(val!=null)
        isValid=val.trim().length()>5 && val.trim().length()<36;
        else isValid=false;
        return isValid;
    }
    
    
    public boolean Apellido(String val){
        if(val!=null)
            return val.trim().length()>0 && val.trim().length()<36;
        return false;
    }


}


